package com.myres.cart.db;


import com.myres.db.ConnectionPool;
import com.myres.model.Order;
import com.myres.model.OrderItem;

import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Set;


public class CartService {
     public Order getCart(){
    	 Connection con=null;
    	 PreparedStatement pstmt=null;
    	 ResultSet rs=null;
    	 try{
    		 con=ConnectionPool.getPool().getConnection();
    		 pstmt=con.prepareStatement("select res_orders.* from"+
    		        " res_orders   "+      
    		       "  where res_orders._status=0 and res_orders._user_id=1");
    		 rs=pstmt.executeQuery();
    		 List<Order> orders=Order.populate(rs);
    		 if(orders==null||orders.size()==0) return null;
    		 Order order=orders.get(0);
    		 pstmt=con.prepareStatement("select * ,(select res_products._name from res_products where res_products._id=_product_id)from res_order_items where _order_id=?");
    		 pstmt.setInt(1, order.getId());
    		 rs=pstmt.executeQuery();
    		 Set<OrderItem> items=OrderItem.populate(rs);
    		 order.setItems(items);
    		 return order;
    	 }catch(Exception e){
    		 e.printStackTrace();
    		 return null;
    	 }finally{
    		 try{
    		     rs.close();
    		     pstmt.close();
    		     con.close();
    		 }catch(Exception e){
    		 }
    		 
    	 }
     }
     
     public int deleteItem(int id){
    	 Connection con=null;
    	 PreparedStatement pstmt=null;
    	 
    	 try{
    		 con=ConnectionPool.getPool().getConnection();
    		 pstmt=con.prepareStatement("delete from res_order_items where _id=?");
    		 pstmt.setInt(1, id);
    		 return pstmt.executeUpdate();
    	 }catch(SQLException e){
    		 e.printStackTrace();
    		 return -1;
    	 }finally{
    		 try{
    			 con.close();
    		 }catch(SQLException e){
    			 
    		 }
    	 }
     }
     public int updateLogisticsInfo(Order order){
    	 Connection con=null;
    	 PreparedStatement pstmt=null;
    	 try{
    	 con=ConnectionPool.getPool().getConnection();
    	 if(order.getBulkId()!=0){
    	     pstmt=con.prepareStatement("update res_orders  set _payment_mode=?,_send_to_place=?,_send_to_who=?,_receiver_phone=? ,_deadLine=? ,_payment=? ,_special_want=? ,_bulk_id=?  where _no=?");
    	 }else{
    		 pstmt=con.prepareStatement("update res_orders  set _payment_mode=?,_send_to_place=?,_send_to_who=?,_receiver_phone=? ,_deadLine=? ,_payment=? ,_special_want=?  where _no=?");
    	 }
    	 pstmt.setInt(1,order.getPaymentMode());
    	 pstmt.setString(2, order.getSendToPlace());
    	 pstmt.setString(3, order.getSendToWho());
    	 pstmt.setString(4, order.getReceiverPhone());
    	 pstmt.setTimestamp(5, order.getDeadline());
    	 pstmt.setFloat(6, order.getPayment());
    	 pstmt.setString(7, order.getSpecial_want());
    	 if(order.getBulkId()!=0){
    		 pstmt.setInt(8, order.getBulkId());
    		 pstmt.setString(9, order.getNO());
    	 }else{
    		 pstmt.setString(8, order.getNO());
    	 }
    	
    	 
    	 int result=pstmt.executeUpdate();
    	 return result;
    	 }catch(SQLException e){
    		 e.printStackTrace();
    		 return -1;
    	 }finally{
    		 try{
    		 con.close();
    		 }catch(SQLException e){
    			 System.out.println("close db con failed");
    		 }
    	 }
    	
    	 
    	 
    	 
     }
}

